<template>
  <div>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top: 30px">

  <el-form-item label="旧密码" prop="oldPassword">
    <el-input v-model="ruleForm.oldPassword" type="password" style="width: 200px"></el-input>
  </el-form-item>
  <el-form-item label="新密码" prop="newPassword">
    <el-input v-model="ruleForm.newPassword" type="password" style="width: 200px"></el-input>
  </el-form-item>
  <el-form-item label="确认新密码" prop="affirmPassword">
    <el-input v-model="ruleForm.affirmPassword" type="password" style="width: 200px"></el-input>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
  </el-form-item>
</el-form>
  </div>
</template>
<script>
  import {updateAdminPwd} from '@/api/system'
  import { removeToken } from '@/utils/auth'
  export default {
    data() {
      return {
        ruleForm: {
          oldPassword:'',
          newPassword:'',
          affirmPassword:''
        },
        rules: {
          oldPassword: [
            { required: true, message: '请输入旧密码', trigger: 'blur' },
          ],

          newPassword: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
          ],
          affirmPassword: [
            { required: true, message: '请确认新密码', trigger: 'blur' },
          ],

        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.ruleForm.affirmPassword!=this.ruleForm.newPassword){
              this.$message.error('两次输入密码不一致');
              return
            }
            let data={
              oldPassword:this.ruleForm.oldPassword,
              newPassword:this.ruleForm.newPassword
            }
            updateAdminPwd(data).then(res =>{
              this.$notify({
                title: 'Success',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              setTimeout(function () {
                removeToken()
                window.location.reload()
              },1000)

            })


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
